﻿using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace FilesNameCopier
{
  public partial class CopierForm : Form
  {
    private DateTime lastEscape;

    public CopierForm()
    {
      InitializeComponent();
    }

    private void Element_DragEnter(object sender, DragEventArgs e)
    {
      e.Effect = DragDropEffects.Copy;
    }

    private void Element_DragDrop(object sender, DragEventArgs e)
    {
      try
      {
        string[] formats = e.Data.GetFormats();

        if (formats.Contains("FileDrop"))
        {
          FileNameMode mode;
          switch (((Control)sender).Tag.ToString())
          {
            case "1":
              mode = FileNameMode.FullPath;
              break;
            case "2":
              mode = FileNameMode.LongName;
              break;
            case "3":
            default:
              mode = FileNameMode.ShortName;
              break;
          }
          Program.ProcessFiles((string[])e.Data.GetData("FileDrop"), mode, this);
        }
      }
      catch (Exception ex)
      {
        Clipboard.SetText("Error: " + ex.Message);
      }
    }

    private void CopierForm_Resize(object sender, EventArgs e)
    {
      ArrangeLabels();
    }

    private void CopierForm_Load(object sender, EventArgs e)
    {
      try
      {
        this.TopMost = FilesNameCopier.Properties.Settings.Default.TopMost;
        this.Size = FilesNameCopier.Properties.Settings.Default.Size;
        this.Location = FilesNameCopier.Properties.Settings.Default.Location;
      }
      catch (Exception) { }
      ArrangeLabels();
    }

    private void CopierForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      if (this.WindowState == FormWindowState.Normal)
      {
        FilesNameCopier.Properties.Settings.Default.Size = this.Size;
        FilesNameCopier.Properties.Settings.Default.Location = this.Location;

        FilesNameCopier.Properties.Settings.Default.Save();
      }
    }

    private void CopierForm_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == 27)
      {
        e.Handled = true;
        DateTime now = DateTime.Now;
        if (lastEscape.AddMilliseconds(SystemInformation.DoubleClickTime) > now)
          this.Close();
        else
          lastEscape = now;
      }
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Program.ShowAbout(this);
    }

    private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
    {
      ShowOptions();
    }

    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    private void ShowOptions()
    {
      using (OptionsForm options = new OptionsForm())
        options.ShowDialog(this);

      this.TopMost = FilesNameCopier.Properties.Settings.Default.TopMost;
    }

    private void ArrangeLabels()
    {
      int menuHeight = mainMenuStrip.Height;
      int w = this.ClientSize.Width - 24;
      int h = (this.ClientSize.Height - (36 + menuHeight)) / 3;

      if (w < 200) w = 200;
      if (h < 40) h = 40;

      labelFullName.Bounds = new Rectangle(12, 9 + menuHeight, w, h);
      labelLongName.Bounds = new Rectangle(12, 18 + menuHeight + h, w, h);
      labelShortName.Bounds = new Rectangle(12, 27 + menuHeight + 2 * h, w, h);
    }

  }
}